$(document).ready(function(){
	var token=localStorage.getItem("token");
	var id = tl_config.his_url;
	id = id.substring(id.indexOf("id=")+3, id.length);
	id = id.substring(0, id.indexOf("&"));
	$.ajax({
		url:server_url+"rest/assignMenu.do?ran="+Math.random(),
		type:"get",
		data:{
			postId:id,
			token:token,
		},
		success:function(data){
			if(data.retCode='0000'){
				var settings={
						keyName:"id",
						parentKeyName:"parentId",
						childKeyName:"children"
					};
				var allMenu=data.data.allMenu;
				var selectedMenu=data.data.selectedMenu;
				for(var i in selectedMenu){
					for(var j in allMenu){
						if(selectedMenu[i].id==allMenu[j].id){
							allMenu[j]['isChecked']="true";
						}
					}
				}
				var nodes=transformTozTreeFormat(settings,allMenu);
				$.fn.tree.init(nodes);
			}else {
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.retMsg
					},
					autoClose : 2000
				});					
			}
		}
	});
	
//	保存并进入下一步
	$(".l_bcgonext").click(function(){
		var level=0;
		$("input:checkbox:checked").each(function(i,v){
			level=$(v).attr("level");
		});
		//如果只勾选了第一层系统则弹出提示
		if(1==level){
			easyDialog.open({
				container:{
					header:'温馨提示',
					content:'请选择分配菜单'
				},
				autoClose : 2000
			});	
			return;
		}
		var menuIdArr=new Array();
		$('input:checkbox:checked').each(function(i,v){
			//获取选中的菜单id
			menuIdArr.push($.parseJSON($(v).val()).id);
		});
		$.ajax({
			type:"post",
			url:server_url+"rest/saveMenu.do?ran="+Math.random(),
			traditional:true,
			data:{
				postId:id,
				menuIdArr:menuIdArr,
				token:token
			},
			success:function(data){
				if(data.retCode='0000'){
					var url='assignOperation.do?id='+id+'&token='+token+'&ran='+Math.random();
					tl_config.loadToWorkArea(url);
				}else {
					easyDialog.open({
						container:{
							header:'温馨提示',
							content:data.retMsg
						},
						autoClose : 2000
					});					
				}
			}
			
		});
	});
	
//	返回
	$(".z_goback").click(function(){
		var url='postList.do?ran='+Math.random();
		tl_config.loadToWorkArea(url);
	});
});